//
//  FindViewController.m
//  Travel assistant
//
//  Created by 杨帆 on 16/2/27.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "FindViewController.h"
#import "FindGenerTableViewCell.h"
#import "FindController.h"
#import "Find.h"
#import "DataHelper.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
@interface FindViewController ()

@property (nonatomic,strong) NSMutableArray *dataArray;

@end

NSInteger number = 2;
@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"发现";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FindGenerTableViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
    //取消纵向滑动条显示
    self.tableView.showsVerticalScrollIndicator = NO;
    
    //解析
    [HELP requestURL:k_find tableView:^(NSMutableArray *arry) {
        self.dataArray = arry;
        
        [self.tableView reloadData];
    }];
    
    //上下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadAction)];
    [self.tableView.mj_footer beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FindGenerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    
    Find *find = self.dataArray[indexPath.row];
    
    cell.titleLabel.text = find.title;
    
    [cell.pictureImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://travelbox-interface.xyz.cn%@",find.url]]];
    
    
    return cell;
    // Configure the cell...
    
}

- (void)viewWillAppear:(BOOL)animated{
    //传回collectionCell的下标。tabCell跳转到特定的
    if (HELP.flag) {
    [self.tableView selectRowAtIndexPath:HELP.index animated:YES scrollPosition:(UITableViewScrollPositionMiddle)];
        
        NSLog(@"----------------------%@",HELP.index);
    [self.tableView reloadData];
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FindController *F = [[FindController alloc]init];
    //点击tabCell跳转传下标
    F.index = indexPath;
    //传数组
    F.dataArray = self.dataArray;
    
    [self.navigationController pushViewController:F animated:YES];
    
    self.tabBarController.tabBar.hidden = YES;
    
    
}

//懒加载
- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

//刷新方法
- (void)refreshAction{
    [self.tableView reloadData];
    
    [self.tableView.mj_header endRefreshing];
    
}


- (void)loadAction{
    //下拉加载的时候解析方法
    [HELP requestURL:number t:^(NSMutableArray *arry) {
        
        for (Find *f in arry) {
            
            [self.dataArray addObject:f];
            
        }
        number++;
    }];
    dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
            
        });
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
